package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.message.queue.DelayQueueMessage;
import com.sduept.message.queue.DelayQueueMessageManager;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 *延迟消息维护controller
 */
@Named
@ViewScoped
public class DelayMessageMaintainController extends AbstractController {
	private static final long serialVersionUID = 3389065766381526877L;
	
	@Autowired
	private DelayQueueMessageManager dqm;

	private List<DelayQueueMessage> delayList = new ArrayList<>();
	private DelayQueueMessage selected = null;
	
	
	@PostConstruct
	public void init() {
		initTable();
	}
	private void initTable()  {
		delayList = dqm.findAllMessage();
	}

	public void query() {
		initTable();
	}
    
	/**
	 * 编辑更新  更新延迟时间、过期时间
	 */
	public void save() {
		if (selected.getId() == null) {
           addErrorMessage("请选择一条数据");
           return;
		}
		long expireTime = selected.getStartTime()+selected.getDelayTime();
		selected.setExpireTime(expireTime);
		try {
			dqm.changeDelayTime(selected);
		}catch(Exception e) {
			addErrorMessage("更新失败");
		}
		query();
		addSuccessMessage("保存成功");
	}

	public void delete() {
		if(selected.getId()==null) {
			 addErrorMessage("请选择一条数据");
	         return;
		}
		selected.setStatus(1);//逻辑删除
		try {
			dqm.deleteDelayQueueMessage(selected);
		}catch(Exception e) {
			addErrorMessage("删除失败");
		}
		addSuccessMessage("删除成功");
		selected = null;
		query();
	}
	public void onRowSelect(SelectEvent event) {
		selected = (DelayQueueMessage) event.getObject();
	}

	public List<DelayQueueMessage> getDelayList() {
		return delayList;
	}

	public void setDelayList(List<DelayQueueMessage> delayList) {
		this.delayList = delayList;
	}


	public DelayQueueMessage getSelected() {
		return selected;
	}


	public void setSelected(DelayQueueMessage selected) {
		this.selected = selected;
	}

}
